package com.sub.spark.sql.source;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.SparkSession;

/**
 * @ClassName JSONSource
 * @Description: 从JSON文件读取数据，按行保证JSON格式
 * @Author Submerge.
 * @Since 2025/5/24 14:29
 * @Version 1.0
 */
public class JSONSource {
    public static void main(String[] args) {
        SparkSession sparkSession = SparkSession.builder().appName("sub-spark-json").master("local[2]").getOrCreate();

        Dataset<Row> json = sparkSession.read().json("data/demo/spark/user.json");
        json.show();


        //保存到JSON文件
        json.write().mode(SaveMode.Overwrite).json("data/demo/output/user");

        //保存到CSV文件
        json.write().mode(SaveMode.Append).csv("data/demo/output/user");

        //保存到Parquet文件
        json.write().mode(SaveMode.Append).parquet("data/demo/output/user");

        sparkSession.stop();

    }
}
